//
//  AppDelegate.m
//  XHNotificationManager
//
//  Created by 曾 宪华 on 13-12-10.
//  Copyright (c) 2013年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507. All rights reserved.
//

#import "AppDelegate.h"
#import "XHNotificationManager.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    /*
    一. 应用还没有运行 直接点启动
    1)、这时如果点击通知的显示按钮，会调用didFinishLaunchingWithOptions和didReceiveRemoteNotification两个方法。这里launchOptions 不会为空的
    
    2)、这时如果点击关闭通知， 再点击应用App  会调用didFinishLaunchingWithOptions，这里launchOptions会为空的，就是正常启动了
     
    二. 应用在后台运行着
    1)、直接点启动 会调用 application:didReceiveRemoteNotification:

    2)、点击关闭通知再点击应用，则上面两个方法都不会被调用这时，只能在applicationWillEnterForeground或者applicationDidBecomeActive方法里,根据发过来通知中的badge进行判断了。
    
    需要注意的是，应用在后台运行一段时间后，有可能被系统终止，这时再接收通知，情况就与第一条一样了。
    
    */
    [XHNotificationManager registerTheAppForPushNotifications:application WithNotificationType:kXHAPNSNotification];
    [XHNotificationManager showAlertMessageForLaunchOptionsDictionary:launchOptions];
    
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString* oldToken = [XHNotificationManager deviceToken];
    
	NSString* newToken = [deviceToken description];
	newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
	newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (![newToken isEqualToString:oldToken]) {
        /**
         这里是不同的token的时候，要上传了
         */
        /**
         *  自己定制上传格式
         */
        NSLog(@"My token is: %@", deviceToken);
        NSLog(@"%@",NSStringFromSelector(_cmd));
        NSLog(@"deviceToken NSData: %@", deviceToken);
        
        NSURL *url = [NSURL URLWithString:@"URL"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        
        NSString *tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
        NSString *noSpaceString = [tokenString stringByReplacingOccurrencesOfString:@" " withString: @""];
        NSString *str = [NSString stringWithFormat:@"token=%@",noSpaceString];
        
        NSLog(@"str token: %@",str);
        [request setHTTPBody:[str dataUsingEncoding: NSUTF8StringEncoding]];
        [request setHTTPMethod:@"POST"];
        [XHNotificationManager sendTokenToServerURLRequest:request completionBlock:^(id json, NSError *error) {
            NSLog(@"json : %@", json);
            [XHNotificationManager setDeviceToken:newToken];
        }];
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [XHNotificationManager showReceivedRemoteNotificationForapplication:application UserInfo:userInfo];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

@end
